package org.cyz.tools.common.util;

import com.alibaba.fastjson.JSON;
import javafx.scene.control.TextArea;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.KeyEvent;
import org.cyz.tools.common.bean.ResultBean;
import org.cyz.tools.common.bean.Settings;

/**
 * @author abccy
 * @title: KeyBoardUtil
 * @projectName RedisGUI
 * @description: TODO
 * @date 2020-9-1820:24
 */
public class KeyBoardUtil {

    /**
     * 0:String 1:Url 2:Html 3:Rtf 4:image 5:Files 6:putFilesByPath
     **/
    public enum Clip {

        STRING(0, DataFormat.PLAIN_TEXT), URL(1, DataFormat.URL), HTML(2, DataFormat.HTML), RTF(3, DataFormat.RTF), IMAGE(4, DataFormat.IMAGE), FILES(5, DataFormat.FILES), FILES_BY_PATH(6, DataFormat.FILES);

        private int index;
        private DataFormat dataFormat;

        Clip(int i, DataFormat files) {
            this.index = i;
            this.dataFormat = files;
        }

        @Override
        public String toString() {
            return "Clip{" +
                    "index=" + index +
                    ", dataFormat=" + dataFormat +
                    '}';
        }

        public static DataFormat getDataFormat(int index) {
            Clip[] clips = Clip.values();
            for (Clip clip : clips) {
                if (index == clip.index) {
                    return clip.dataFormat;
                }
            }
            return null;
        }
    }

    /**
     * 输入事件处理
     *
     * @param keyEvent
     * @param inputConsole
     */
    public void inputEvent(KeyEvent keyEvent, TextArea inputConsole) {
        switch (keyEvent.getCode()) {
            case BACK_SPACE:
            case ENTER:
                //+ System.getProperty("line.separator")
                //处理 > 符号能被删除的问题
                if (inputConsole.getText().split(">").length <= 1) {
                    keyEvent.consume();
                }
                //处理连续输入后按回车键会执行bug
                if(!(inputConsole.getText().split(">").length > 1&&inputConsole.getText().split("\n")[inputConsole.getText().split("\n").length-1].split(">").length>1)) {
                    keyEvent.consume();
                }
                    break;
            default:

                break;
        }
    }

    /**
     * 输入内容
     * @param keyEvent
     * @param inputConsole
     * @param showConsole
     */
    public void inputContent(KeyEvent keyEvent, TextArea inputConsole, TextArea showConsole) {
        switch (keyEvent.getCode()){
            case ENTER:
                if(inputConsole.getText().split(">").length > 1&&inputConsole.getText().split("\n")[inputConsole.getText().split("\n").length-1].split(">").length>1){
                    String inputText = inputConsole.getText().split("\n")[inputConsole.getText().split("\n").length - 1] ;
                    showConsole.appendText(ConsoleUtil.inputMsg(inputText.split(">")[1]) +System.getProperty("line.separator"));
                    inputConsole.appendText(ConsoleUtil.consoleHeader());
                }
                break;
        }
    }

        /**
         * 将值设置到剪贴板
         *
         * @param resultBean
         */
    public static void setClipboard(ResultBean resultBean) {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        String result = null;
        switch (resultBean.getValueType()) {
            case "string":
                result = (resultBean.getValue());
                break;
            case "hash":
                result = JSON.toJSONString(resultBean.getHashValue());
                break;
            case "list":
                result = JSON.toJSONString(resultBean.getListValue());
                break;
            case "set":
                result = JSON.toJSONString(resultBean.getSetValue());
                break;
            case "zset":
                break;
        }
        ClipboardContent clipboardContent = new ClipboardContent();
        clipboardContent.putString(result);
        clipboard.setContent(clipboardContent);
    }

    /**
     * 设置内容到剪贴板
     *
     * @param content
     * @param type    0:String 1:Url 2:Html 3:Rtf 4:image 5:Files 6:putFilesByPath
     */
    public static void setClipboard(Object content, int type) {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        ClipboardContent clipboardContent = new ClipboardContent();
        clipboardContent.put(Clip.getDataFormat(type), content);
        clipboard.setContent(clipboardContent);
    }


    public static void toClipboard(ResultBean resultBean) {
        Settings clipSettings = InitDataUtil.getSettings(InitDataUtil.CLIP);
        if (null != clipSettings) {
            if (Boolean.valueOf(clipSettings.getValue())) {
                setClipboard(resultBean);
            }
        }
    }

}
